/**
 * Author: Andrew Shore
 * Date Last Modified: April 10,2009
 * CSCE 146: Homework 06, BTNode.java
 * 
 * Description:
 * Class implementing a Binary Tree node. Each node has a parent node,
 * left child node, right child node, and a Record data payload. This 
 * class has a default and overloaded constructor, accessors and 
 * mutators,and methods that return the height of the node, whether it
 * is internal and external, has a right or left child, and also has 
 * a compareTo method.
 */
public class BTNode implements IBTNode {
		/**
		 * Class variables
		 */
		private Record element;
		private BTNode parent;
		private BTNode left;
		private BTNode right;
		
		/**
		 * Default Constructor
		 */
		public BTNode(){
			
		}
		/**
		 * Overloaded constructor
		 * 
		 * Parameters: rec - Record of this node
		 * 			   par - parent node of this node
		 *             leftChild - left child of this node
		 *             rightChild - right child of this node
		 */
		public BTNode(Record rec, BTNode par, BTNode leftChild, BTNode rightChild){
			this.element = rec;
			this.parent = par;
			this.left = leftChild;
			this.right = rightChild;
		}
		
		/**
		 * Function returns the height of the tree by recursively calling
		 * the height function for the parent node. The height of the root
		 * is 0.
		 */
		public int getHeight() {
			
			if(this.parent != null){
				return (parent.getHeight() + 1); 
			}
			else{
				return 0;
			}
		
		}
		
		/**
		 * Accessors
		 */
		public BTNode getLeft() {
			return left;
		}

		public BTNode getParent() {
			return parent;
		}

		public Record getElement() {
			return element;
		}

		public BTNode getRight() {
			return right;
		}
		/**
		 * Mutators
		 */
		public void setLeft(BTNode v) {
			this.left = v;		
		}

		public void setParent(BTNode v) {
			this.parent = v;
		}

		public void setElement(Record rec) {
			this.element = rec;
		}

		public void setRight(BTNode v) {
			this.right = v;
		}

		/**
		 * Returns true if the node has a left child, else returns false
		 */
		public boolean hasLeft() {
			if(this.getLeft() != null){
				return true;
			}
			else{
				return false;
			}
		}
		
		/**
		 * Returns true if the nod has a right child, else returns false
		 */
		public boolean hasRight() {
			if(this.getRight() != null){
				return true;
			}
			else{
				return false;
			}
		}
		
		/**
		 * Returns true if the node is external, else returns false
		 */
		public boolean isExternal() {
			if(this.isInternal()){
				return false;
			}
			else{
				return true;
			}
		}
		
		/**
		 * Returns true if the node is internal, else returns false
		 */
		public boolean isInternal() {
			if(this.hasLeft() || this.hasRight()){
				return true;
			}
			else{
				return false;
			}
		}

       /**
        * compareTo method for a Binary Tree node. Calls the compareTo method
        * of the data payload(Record class) and returns -1,0,1 if the calling
        * object is less than, equal, or greater than the argument respectively.
        * 
        * Parameters: node - node being compared
        */
		public int compareTo(BTNode node){
			if(element.compareTo(node.getElement()) < 0){
				return -1;
			}else if(element.compareTo(node.getElement()) > 0){
				return 1;
			}else{
				return 0;
			}
		}
}